package com.example.springboot.service.impl;

import com.example.springboot.domain.Like;
import com.example.springboot.domain.User;
import com.example.springboot.mapper.LikeMapper;
import com.example.springboot.service.LikeService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    LikeMapper likeMapper;

//    插入点赞
    @Override
    public void set(Like like) {
        User user = TokenUtils.getCurrentUser();
        like.setUserId(user.getUserid());
        Like like1 = likeMapper.selectUserLike(like);
        if(like1 == null){
            likeMapper.insert(like);
        }
        else {
            likeMapper.deleteById(like1.getId());
        }
    }

//    查询当前用户是否点过赞
    @Override
    public Like selectUserLikes(Integer fid, String module) {
        User user = TokenUtils.getCurrentUser();
        Like likes = new Like();
        likes.setUserId(user.getUserid());
        likes.setFid(fid);
        likes.setModule(module);
        return likeMapper.selectUserLike(likes);
    }

//    查询该博客点赞量
    @Override
    public int selectByFidAndModule(Integer fid, String module) {
        return likeMapper.selectByFidAndModule(fid, module);
    }

    @Override
    public void deleteById(Integer id) {
        likeMapper.deleteById(id);
    }
}
